package Algorithm;

import java.util.HashMap;
import java.util.Scanner;

public class Day02 {
    public static void main1(String[] args) {
        int n = 3;
        int[][] res = new int[n][n];
        int i = 0;
        int j = 0;
        int startx = 0;
        int starty = 0;
        int offset = 1;
        int count = 1;
        int cm = n / 2;
        while(cm > 0) {
            for(j = starty; j < n-offset; j++)
                res[startx][j] = count++;
            for(i = startx; i < n-offset; i++)
                res[i][j] = count++;
            for(; j > starty; j--)
                res[i][j] = count++;
            for(; i > startx; i--)
                res[i][j] = count++;
            startx++;
            starty++;
            offset++;
            cm--;
        }
        if(n % 2 == 1) {
            res[startx][starty] = count;
        }
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++) {
                System.out.print(res[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int[][] field = new int[n][m];
        int sum = 0;

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m ; j++) {
                field[i][j] = scan.nextInt();
                sum += field[i][j];
            }
        }

        //求水平的和
        int[] horizantal = new int[n];
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                horizantal[i] = field[i][j];
            }
        }

        //求列的和
        int[] vertical = new int[m];
        for(int j = 0; j < m; j++) {
            for(int i = 0; i < n; i++) {
                vertical[j] = field[i][j];
            }
        }

        int res = Integer.MAX_VALUE;
        //横向切割
        int horizantalCut = 0;
        for(int i = 0; i < n; i++) {
            //第一个区间和
            horizantalCut += horizantal[i];
            res = Math.min(res, Math.abs(sum - horizantalCut - horizantalCut));

        }

        //纵向切割
        int verticalCut = 0;
        for(int j = 0; j < m; j++) {
            verticalCut += vertical[j];
            res = Math.min(res, Math.abs(sum - verticalCut - verticalCut));
        }
        System.out.println(res);
        HashMap<String, Integer> map = new HashMap<>();
    }
}
